/*
 * ikakeibo 0.6.0
 * Copyright (C)2009 wateray and contributors
 * wateray@gmail.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package com.wateray.ipassbook.ui.dialog;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.MessageFormat;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTable;
import javax.swing.JTextField;

import org.apache.log4j.Logger;

import com.wateray.ipassbook.commom.Constant;
import com.wateray.ipassbook.domain.Entity;
import com.wateray.ipassbook.domain.Receipt;
import com.wateray.ipassbook.domain.Subject;
import com.wateray.ipassbook.domain.form.PassbookForm;
import com.wateray.ipassbook.domain.form.ReceiptForm;
import com.wateray.ipassbook.kernel.event.IPassbookEvent;
import com.wateray.ipassbook.kernel.event.IPassbookEventType;
import com.wateray.ipassbook.kernel.event.IPassbookObserver;
import com.wateray.ipassbook.kernel.service.PassbookService;
import com.wateray.ipassbook.kernel.service.ReceiptService;
import com.wateray.ipassbook.kernel.service.SubjectService;
import com.wateray.ipassbook.ui.action.ActionManager;
import com.wateray.ipassbook.ui.action.AddSubjectAction;
import com.wateray.ipassbook.ui.action.ShowPassbookWizardAction;
import com.wateray.ipassbook.ui.model.PassbookComboBoxModel;
import com.wateray.ipassbook.ui.model.PassbookListCellRender;
import com.wateray.ipassbook.ui.model.ReceiptTabelModel;
import com.wateray.ipassbook.ui.model.SubjectListCellRender;
import com.wateray.ipassbook.ui.view.contentPanel.ReceiptPage;
import com.wateray.ipassbook.ui.widget.TitleMessageDialog;
import com.wateray.ipassbook.util.CalendarUtils;
import com.wateray.ipassbook.util.LanguageLoader;
import com.wateray.ipassbook.util.log4j.Log;

/**
 * @author wateray
 * @create 2009-5-6
 */
public class ReceiptDialog extends TitleMessageDialog {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	/** logger. */
	Logger logger = Log.getLogger();

	public static ReceiptDialog receiptDialog;

	/** layout data. */
	private static int width = 600;
	private static int height = 280;
	private static int centerIndent = 80;

	/** type radioButton. */
	private JLabel typeLabel;
	private ButtonGroup incomeExpenseGroup;
	private JRadioButton incomeRadioButton;
	private JRadioButton expenseRadioButton;

	/** date. */
	private JLabel dateLabel;
	private JTextField dateText;

	/** subject. */
	private JLabel subjectLabel;
	private JComboBox subjectComboBox;
	private JButton subjectAddButtton;
	private PassbookComboBoxModel subjectComboBoxModel;

	/** amount. */
	private JLabel amountLabel;
	private JTextField amountText;

	/** passbook. */
	private JLabel passbookLabel;
	private JComboBox passbookComboBox;
	private JTextField currencyText;
	private JButton passbookAddButtton;
	private PassbookComboBoxModel passbookComboBoxModel;

	/** statistics. */
	private JComboBox statisticsSubjectComboBox;
	private PassbookComboBoxModel statisticsSubjectComboBoxModel;
	private JButton statisticsSubjectAddButton;

	/** memo. */
	private JTextField memoText;

	/** bottom button. */
	private JCheckBox continueCheckBox;

	/** data. */
	private JTable receiptTable;
	private ReceiptTabelModel receiptTabelModel;
	//convertRowIndexToModel
	private int receiptTableSelectedRow = -1;

	/** receiptId. */
	private String receiptId;
	
	//subjectComboBox List Items
	private Vector<Subject> incomeSubjectList;
	
	//subjectComboBox List Items
	private Vector<Subject> expenseSubjectList;

	/***/
	private Type type;

	/**
	 * enmu <b>Type</b> Type.ADD; Type.MoDIFY; Type.DELET.
	 * */
	public enum Type {
		/** Add Receipt. */
		ADD,
		/** Modify Receipt. */
		MODIFY,
		/** Delete Receipt. */
		DELETE;
	}

	/**
	 * Constructor.
	 * 
	 * @param owner
	 *            -- JFrame. The parent of this dialog.
	 * @param type
	 *            -- Type.ADD; Type.MoDIFY; Type.DELET.
	 * */
	public ReceiptDialog(JFrame owner, Type type) {
		this(owner, width, height, true, "", type);
	}

	/**
	 * Constructor.
	 * 
	 * @param owner
	 *            -- JFrame. The parent of this dialog.
	 * @param title
	 *            -- The title.
	 * @param type
	 *            -- Type.ADD; Type.MoDIFY; Type.DELET.
	 * */
	public ReceiptDialog(Component owner, String title, Type type) {
		this(owner, width, height, true, title, type);

	}

	public ReceiptDialog(Component owner, int width, int height, boolean modal,
			String title, Type type) {
		super(owner, width, height, modal, title);
		this.type = type;
		this.putButton(getContinueCheckBox());
		this.layoutButtonPanel();

		receiptTable = ReceiptPage.getInstance().getReceiptTable();
		receiptTabelModel = (ReceiptTabelModel) receiptTable.getModel();
		if(receiptTable.getSelectedRow() != -1){
			receiptTableSelectedRow = receiptTable.convertRowIndexToModel(receiptTable.getSelectedRow());;
		}
		
		initData();
		initState();
	}

	@Override
	public JComponent getCenterPane() {
		if (centerPane == null) {
			centerPane = new JPanel(new GridBagLayout());
			GridBagConstraints c = new GridBagConstraints();
			typeLabel = new JLabel(LanguageLoader.getString("Receipt_Type"));

			/** type. */
			// typeLabel
			c.fill = GridBagConstraints.BOTH;
			c.anchor = GridBagConstraints.PAGE_START;
			c.insets = getDefaultInsets();
			c.gridx = 0;
			c.gridy = 0;
			c.gridwidth = 1;
			c.gridheight = 1;
			centerPane.add(typeLabel, c);

			// incomeRadioButton
			c.gridx = 1;
			centerPane.add(getIncomeRadioButton(), c);

			// expenseRadioButton
			c.gridx = 2;
			centerPane.add(getExpenseRadioButton(), c);

			// incomeExpenseGroup
			incomeExpenseGroup = new ButtonGroup();
			incomeExpenseGroup.add(getIncomeRadioButton());
			incomeExpenseGroup.add(getExpenseRadioButton());

			/** subject. */
			// subjectLabel
			subjectLabel = new JLabel(LanguageLoader.getString("SubjectEntity"));
			c.gridx = 3;
			c.insets = new Insets(0, centerIndent, 0, 0);
			centerPane.add(subjectLabel, c);

			// subjectComboBox
			c.gridx = 4;
			c.insets = new Insets(5, 5, 5, 0);
			;
			c.gridwidth = 2;
			centerPane.add(getSubjectComboBox(), c);

			// subjectAddButton
			c.insets = new Insets(5, 0, 5, 5);
			c.gridx = 6;
			c.gridwidth = 1;
			centerPane.add(getSubjectAddButtton(), c);

			/** date */
			// dateLabel
			dateLabel = new JLabel(LanguageLoader.getString("Date"));
			c.insets = getDefaultInsets();
			c.gridx = 0;
			c.gridy = 1;
			c.gridwidth = 1;
			centerPane.add(dateLabel, c);

			// dateText
			c.gridx = 1;
			c.gridwidth = 2;
			centerPane.add(getDateText(), c);

			/** passbook. */
			// passbookLabel
			passbookLabel = new JLabel(LanguageLoader.getString("Passbook"));
			c.gridx = 3;
			c.gridwidth = 1;
			c.insets = new Insets(0, centerIndent, 0, 0);
			centerPane.add(passbookLabel, c);

			// passbookComboBox
			// c.insets = getDefaultInsets();
			c.insets = new Insets(5, 5, 5, 0);
			c.gridx = 4;
			centerPane.add(getPassbookComboBox(), c);

			// currencyText
			c.insets = new Insets(5, 0, 5, 0);
			c.gridx = 5;
			centerPane.add(getCurrencyText(), c);

			// passbookAddButton
			c.insets = new Insets(5, 0, 5, 5);
			c.gridx = 6;
			c.gridwidth = 1;
			centerPane.add(getPassbookAddButtton(), c);

			/** amount. */
			// amountLabel
			amountLabel = new JLabel(LanguageLoader.getString("Amount"));
			c.insets = getDefaultInsets();
			c.gridx = 0;
			c.gridy = 2;
			centerPane.add(amountLabel, c);

			// amountText
			c.gridx = 1;
			c.gridwidth = 2;
			centerPane.add(getAmountText(), c);

			/** statisticsSubject. */
			// statisticsSubjectLabel
			JLabel statisticsSubjectLabel = new JLabel(LanguageLoader
					.getString("Statistics"));
			c.gridx = 3;
			c.gridwidth = 1;
			c.insets = new Insets(0, centerIndent, 0, 0);
			centerPane.add(statisticsSubjectLabel, c);

			// statisticsSubjectComboBox.
			c.gridx = 4;
			c.gridwidth = 2;
			c.insets = new Insets(5, 5, 5, 0);
			;
			centerPane.add(getStatisticsSubjectComboBox(), c);

			// statisticsSubjectAddButton
			c.insets = new Insets(5, 0, 5, 5);
			;
			c.gridx = 6;
			c.gridwidth = 1;
			centerPane.add(getStatisticsSubjectAddButton(), c);

			/** memo. */
			// memoLabel
			JLabel memoLabel = new JLabel(LanguageLoader.getString("Memo"));
			c.insets = getDefaultInsets();
			c.gridx = 0;
			c.gridy = 3;
			c.gridwidth = 1;
			centerPane.add(memoLabel, c);

			// memoText
			c.gridx = 1;
			c.gridwidth = 5;
			centerPane.add(getMemoText(), c);

			/** initData. */
			initData();
		}
		return centerPane;

	}

	public void initData() {
		ReceiptForm entity = null;

		if (getType() == Type.ADD){
		} 
		else if (getType() == Type.MODIFY || getType() == Type.DELETE) {
			if (receiptTableSelectedRow != -1) {	
				entity = (ReceiptForm) receiptTabelModel.getRowObject(receiptTableSelectedRow);
			}
		}
		if (entity == null) {
			// type
			getExpenseRadioButton().setSelected(true);

			// subject
			/**
			 * -1 indicates no selection.
			 * */
			getSubjectComboBox().setSelectedIndex(-1);

			// date
			getDateText().setText(
					CalendarUtils.getTodayInstance().getBeginDay());

			// passbook
			getPassbookComboBox().setSelectedIndex(-1);

			// amount
			getAmountText().setText("");

			// statisticsSubjectComboBox()
			getStatisticsSubjectComboBox().setSelectedIndex(-1);

			// memo
			getMemoText().setText("");

		} else {
			// receiptId
			receiptId = entity.getReceiptId();

			// type
			getIncomeRadioButton().setSelected(
					entity.getReceiptFlag().equals(Constant.INCOME_KBN));
			getExpenseRadioButton().setSelected(
					entity.getReceiptFlag().equals(Constant.EXPENSE_KBN));

			// subject
			Vector<Subject> data = new SubjectService()
					.getParentAndChildrenListByFlag(getIncomeRadioButton()
							.isSelected() ? Constant.INCOME_KBN
							: Constant.EXPENSE_KBN);
			((PassbookComboBoxModel) getSubjectComboBox().getModel())
					.setData(data);
			((PassbookComboBoxModel) getSubjectComboBox().getModel())
					.setSelectedObjectById(entity.getSubjectId());
			// date
			getDateText().setText(entity.getReceiptDay());

			// passbook
			((PassbookComboBoxModel) getPassbookComboBox().getModel())
					.setSelectedObjectById(entity.getPassbookDetailId());

			// currencyTex (It's not need to set this.)
			// getCurrencyText().setText(entity.getCurr_nm());

			// amount
			getAmountText().setText(String.valueOf(entity.getAmount()));

			// statisticsSubjectComboBox()
			// #temp
			((PassbookComboBoxModel) getStatisticsSubjectComboBox().getModel())
					.setSelectedObjectById(entity.getSubjectId());

			// memo
			getMemoText().setText(entity.getMemo());
		}

		// continueCheckBox
		if (getType() != Type.ADD) {
			getContinueCheckBox().setSelected(false);
		} else {
			getContinueCheckBox().setSelected(true);
		}

	}

	/** initState. */
	public void initState() {
		if (getType() == Type.ADD) {

		} else if (getType() == Type.MODIFY) {

		} else if (getType() == Type.DELETE) {
			for (Component componet : getCenterPane().getComponents()) {
				if (componet instanceof JLabel) {
					// Label needn't to disable. Do nothing.
				} else {
					componet.setEnabled(false);
				}
			}

			// disable continueCheckBox
			// getContinueCheckBox().setEnabled(false);
			// invisible continueCheckBox
			getContinueCheckBox().setVisible(false);
		}
	}

	// process data.
	private void processData() {
		boolean flag = checkData();
		this.setMessageLabel(errorMessage);
		if (!flag) {
			return;
		}

		// process data start.
		Receipt receipt = new Receipt();

		// receiptId
		receipt.setReceiptId(receiptId);
		// subject
		receipt.setSubjectId(subjectComboBoxModel.getSelectedObject()
				.getEntityId());
		// date
		receipt.setReceiptDay(dateText.getText().trim());

		// passbook
		PassbookComboBoxModel passbookModel = (PassbookComboBoxModel) getPassbookComboBox()
				.getModel();
		PassbookForm passbookForm = (PassbookForm) passbookModel
				.getSelectedObject();

		receipt.setPassbookId(passbookForm.getPassbookId());
		receipt.setPassbookSubId(passbookForm.getPassbookSubId());
		// currency
		receipt.setCurrencyId(passbookForm.getCurrencyId());
		// amount
		receipt.setAmount(Double.parseDouble(amountText.getText().trim()));
		// memo
		receipt.setMemo(memoText.getText());
		// KBN
		receipt
				.setReceiptFlag(incomeRadioButton.isSelected() ? Constant.INCOME_KBN
						: Constant.EXPENSE_KBN);
		// userID
		// inExEntity.setIn_ex_user_id("wateray");

		//
		ReceiptService receiptService = new ReceiptService();

		if (getType() == Type.ADD) {
			// receiptId = inExDAO.insert(inExEntity);
			receiptId = receiptService.insertReceipt(receipt);

			// invoke receiptTabelModel
			receiptTabelModel.addRow(receiptService
					.getReceiptFormById(receiptId));

		} else if (getType() == Type.MODIFY) {
			//
			receiptService.updateReceipt(receipt);
			
			// invoke receiptTabelModel
			receiptTabelModel.updateRow(receiptTableSelectedRow,
					receiptService.getReceiptFormById(receiptId));
		} else if (getType() == Type.DELETE) {
			receiptService.deleteReceipt(receipt);

			// invoke receiptTabelModel
			receiptTabelModel.removeRow(receiptTableSelectedRow);
		}

		// process data end.

		// Finished.
		if (!continueCheckBox.isSelected()) {
			// Close this dialog.
			this.dispose();
		} else {
			// Continue to add record.
			initData();
		}

	}

	private boolean checkData() {
		errorMessage = "";

		// check subject
		PassbookComboBoxModel subjectComboBoxModel = (PassbookComboBoxModel) getSubjectComboBox()
				.getModel();
		Subject subject = (Subject) subjectComboBoxModel
				.getObjectAt(getSubjectComboBox().getSelectedIndex());
		if (subject == null) {
			errorMessage = MessageFormat.format(LanguageLoader
					.getString("empty"), LanguageLoader
					.getString("SubjectEntity"));
			getSubjectComboBox().requestFocus();
			return false;
		} else {
			// If has child,can not selected.
			if (subject.hasChild()) {
				errorMessage = MessageFormat.format(LanguageLoader
						.getString("canNotSelected"), getSubjectComboBox()
						.getSelectedItem());
				getSubjectComboBox().requestFocus();
				return false;
			}
		}

		// check date
		// 1. empty
		if (getDateText().getText().trim().equals("")) {
			errorMessage = MessageFormat.format(LanguageLoader
					.getString("empty"), dateLabel.getText());
			getDateText().requestFocus();
			return false;
		}
		// 2.Date format
		// try{
		// SimpleDateFormat.getInstance().parse(dateText.getText().trim());
		// }
		// catch(ParseException pe){
		// errorMessage = MessageFormat.format(LanguageLoader
		// .getString("DateParseException"), dateLabel.getText());
		// getDateText().requestFocus();
		// return false;
		// }

		// check passbook
		if (getPassbookComboBox().getSelectedItem() == null) {
			errorMessage = MessageFormat.format(LanguageLoader
					.getString("empty"), passbookLabel.getText());
			getPassbookComboBox().requestFocus();
			return false;
		}

		// check amount
		// empty
		if (getAmountText().getText().trim().equals("")) {
			errorMessage = MessageFormat.format(LanguageLoader
					.getString("empty"), amountLabel.getText());
			getAmountText().requestFocus();
			return false;
		}
		// number
		try {
			Double.parseDouble(amountText.getText().trim());
		} catch (NumberFormatException nfe) {
			errorMessage = MessageFormat.format(LanguageLoader
					.getString("NumberFormatException"), amountLabel.getText());
			getAmountText().requestFocus();
			return false;
		}

		return true;
	}

	private JTextField getDateText() {
		if (dateText == null) {
			dateText = new JTextField();
		}
		return dateText;
	}

	// currencyText
	private JTextField getCurrencyText() {
		if (currencyText == null) {
			currencyText = new JTextField();
			currencyText.setEditable(false);
			currencyText.setMinimumSize(new Dimension(40, 0));
			currencyText.setPreferredSize(new Dimension(40, 0));
		}

		return currencyText;
	}

	private JRadioButton getIncomeRadioButton() {
		if (incomeRadioButton == null) {
			incomeRadioButton = new JRadioButton(LanguageLoader
					.getString("Income"));
			incomeRadioButton.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent event) {
					HandleActionPerformed(event);
				}
			});
			incomeRadioButton.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent e) {
					HandleMouseClicked(e);
				}
			});

		}
		return incomeRadioButton;
	}

	private JRadioButton getExpenseRadioButton() {
		if (expenseRadioButton == null) {
			expenseRadioButton = new JRadioButton(LanguageLoader
					.getString("Expense"));
			expenseRadioButton.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent event) {
					HandleActionPerformed(event);
				}
			});
			expenseRadioButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					HandleActionPerformed(event);
				}
			});
			expenseRadioButton.doClick();
		}
		return expenseRadioButton;
	}

	private JComboBox getSubjectComboBox() {
		if (subjectComboBox == null) {
			subjectComboBox = new JComboBox();
			subjectComboBox.setMinimumSize(new Dimension(160, 0));
			subjectComboBox.setPreferredSize(new Dimension(160, 0));

			subjectComboBox.setModel(getSubjectComboBoxModel());
			subjectComboBox.setRenderer(new SubjectListCellRender());
		}

		return subjectComboBox;
	}

	private JTextField getAmountText() {
		if (amountText == null) {
			amountText = new JTextField();
			amountText.setHorizontalAlignment(JTextField.RIGHT);
		}
		return amountText;
	}

	private PassbookComboBoxModel getSubjectComboBoxModel() {
		if (subjectComboBoxModel == null) {
			subjectComboBoxModel = new PassbookComboBoxModel();
		}
		return subjectComboBoxModel;
	}

	private JComboBox getPassbookComboBox() {
		if (passbookComboBox == null) {
			passbookComboBox = new JComboBox();
			passbookComboBox.setMinimumSize(new Dimension(120, 0));
			passbookComboBox.setPreferredSize(new Dimension(120, 0));
			// passbookComboBoxModel = new PassbookComboBoxModel();
			passbookComboBox.setModel(getPassbookComboBoxModel());
			passbookComboBox.setRenderer(new PassbookListCellRender());

			passbookComboBox.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					passbookComboBoxActionPerformed(event);
				}
			});
		}
		return passbookComboBox;
	}

	private PassbookComboBoxModel getPassbookComboBoxModel() {
		if (passbookComboBoxModel == null) {
			passbookComboBoxModel = new PassbookComboBoxModel();
			passbookComboBoxModel.setData(getData());
		}
		return passbookComboBoxModel;
	}

	private Vector<Subject> getIncomeSubjectList(){
		if(incomeSubjectList == null){
			incomeSubjectList = new SubjectService()
			.getParentAndChildrenListByFlag(Constant.INCOME_KBN);
		}
		return incomeSubjectList;
	}
	private Vector<Subject> getExpenseSubjectList(){
		if(expenseSubjectList == null){
			expenseSubjectList = new SubjectService()
			.getParentAndChildrenListByFlag(Constant.EXPENSE_KBN);
		}
		return expenseSubjectList;
	}
	
	private Vector<Entity> getData() {
		Vector<Entity> vect = new Vector<Entity>();
		PassbookService service = new PassbookService();
		List result = service.getPassbookFormList();
		Iterator e = result.iterator();
		while (e.hasNext()) {
			vect.add((Entity) e.next());
		}
		return vect;
	}

	private JComboBox getStatisticsSubjectComboBox() {
		if (statisticsSubjectComboBox == null) {
			statisticsSubjectComboBox = new JComboBox();
			statisticsSubjectComboBox
					.setModel(getStatisticsSubjectComboBoxModel());
			statisticsSubjectComboBox.setRenderer(new SubjectListCellRender());
		}
		return statisticsSubjectComboBox;
	}

	private PassbookComboBoxModel getStatisticsSubjectComboBoxModel() {
		if (statisticsSubjectComboBoxModel == null) {
			statisticsSubjectComboBoxModel = new PassbookComboBoxModel();
		}
		return statisticsSubjectComboBoxModel;
	}

	private JCheckBox getContinueCheckBox() {
		if (continueCheckBox == null) {
			continueCheckBox = new JCheckBox(LanguageLoader
					.getString("Continue_Add"));
			// continueCheckBox.doClick();
		}
		return continueCheckBox;
	}

	private JTextField getMemoText() {
		if (memoText == null) {
			memoText = new JTextField();
		}
		return memoText;
	}

	private Insets getDefaultInsets() {
		return new Insets(5, 5, 5, 5);
	}

	private void HandleActionPerformed(ActionEvent event) {
		Handler(event.getSource());
	}

	private void HandleMouseClicked(MouseEvent event) {
		Handler(event.getSource());
	}

	private void Handler(Object source) {
		if (source.equals(incomeRadioButton)) {
//			Vector<Subject> data = new SubjectService()
//					.getParentAndChildrenListByFlag(Constant.INCOME_KBN);
			getSubjectComboBoxModel().setData(getIncomeSubjectList());
			getStatisticsSubjectComboBoxModel().setData(getIncomeSubjectList());
		} else if (source.equals(expenseRadioButton)) {
//			Vector<Subject> data = new SubjectService()
//					.getParentAndChildrenListByFlag(Constant.EXPENSE_KBN);
			getSubjectComboBoxModel().setData(getExpenseSubjectList());
			getStatisticsSubjectComboBoxModel().setData(getExpenseSubjectList());
		}
	}

	private void passbookComboBoxActionPerformed(ActionEvent event) {
		PassbookForm pb = (PassbookForm) passbookComboBoxModel
				.getSelectedObject();
		if (pb != null) {
			currencyText.setText(pb.getCurrencyName());
		} else {
			currencyText.setText("");
		}

	}

	@Override
	protected void finishButtonActionPerformed(ActionEvent event) {
		processData();
	}

	/**
	 * @return the type
	 */
	public Type getType() {
		return type;
	}

	/**
	 * @param type
	 *            the type to set
	 */
	public void setType(Type type) {
		this.type = type;
	}

	/**
	 * @return the subjectAddButtton
	 */
	private JButton getSubjectAddButtton() {
		if (subjectAddButtton == null) {
			subjectAddButtton = new JButton(ActionManager
					.getAction(AddSubjectAction.class));
			subjectAddButtton.setText("");
		}
		return subjectAddButtton;
	}

	/**
	 * @return the passbookAddButtton
	 */
	private JButton getPassbookAddButtton() {
		if (passbookAddButtton == null) {
			passbookAddButtton = new JButton(ActionManager
					.getAction(ShowPassbookWizardAction.class));
			passbookAddButtton.setText("");
		}
		return passbookAddButtton;
	}

	/**
	 * @return the statisticsAddButton
	 */
	private JButton getStatisticsSubjectAddButton() {
		if (statisticsSubjectAddButton == null) {
			statisticsSubjectAddButton = new JButton(ActionManager
					.getAction(AddSubjectAction.class));
			statisticsSubjectAddButton.setText("");
		}
		return statisticsSubjectAddButton;
	}

	@Override
	public void update(IPassbookEvent event) {
		// TODO Auto-generated method stub

		// subjectComboBoxModel and statisticsSubjectComboBoxModel list'data
		// update.
		// if(event.getEventType() == IPassbookEventType.SubjectChangeEvent){
		if (event.getEventType().isAlso(
				IPassbookEventType.SubjectTableChangeEvent)) {
			incomeSubjectList = null;
			expenseSubjectList = null;
			if (getIncomeRadioButton().isSelected()) {
				getIncomeRadioButton().doClick();
			} else {
				getExpenseRadioButton().doClick();
			}
		}
		if (event.getEventType().isAlso(
				IPassbookEventType.PassbookTableChangeEvent)) {
			passbookComboBoxModel.setData(getData());

		}

	}

}
